public class T3_0906_0514 {
    public static char ret = 'A';
    public static void main(String[] args) {
        Object lock = new Object();
        Thread t1 = new Thread(() -> {
            int n = 10;
            while (n-- > 0) {
                  synchronized (lock) {
                      while (ret != 'A') {
                          try {
                              lock.wait();
                          } catch (InterruptedException e) {
                              throw new RuntimeException(e);
                          }
                      }
                      System.out.print(ret);
                      ret = 'B';
                      lock.notifyAll();
                  }
           }
        });
        Thread t2 = new Thread(() -> {
            int n = 10;
            while (n-- > 0) {
                synchronized (lock) {
                    while (ret != 'B') {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.print(ret);
                    ret = 'C';
                    lock.notifyAll();
                }
            }
        });
        Thread t3 = new Thread(() -> {
            int n = 10;
            while (n-- > 0) {
                synchronized (lock) {
                    while (ret != 'C') {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println(ret);
                    ret = 'A';
                    lock.notifyAll();
                }
            }
        });

        t1.start();
        t2.start();
        t3.start();
    }
}
